<template>
  <div>
    <input type="file" accept="image/jpeg,image/jpg,image/png,image/gif" @change="upload">
    <img :src="src" alt="">
    <hr>
    <el-upload
      action="#"
      list-type="picture-card"
      :auto-upload="false"
      :limit="3"
      :on-change="handleChange"
      :file-list="fileList"
      :class="{hidden:fileList.length === 3}"
    >
      <i slot="default" class="el-icon-plus" />
      <div slot="file" slot-scope="{file}">
        <img
          class="el-upload-list__item-thumbnail"
          :src="file.url"
          alt=""
        >
        <span class="el-upload-list__item-actions">
          <span
            class="el-upload-list__item-preview"
            @click="handlePictureCardPreview(file)"
          >
            <i class="el-icon-zoom-in" />
          </span>
          <span
            v-if="!disabled"
            class="el-upload-list__item-delete"
            @click="handleRemove(file)"
          >
            <i class="el-icon-delete" />
          </span>
        </span>
      </div>
    </el-upload>
    <el-progress v-show="percent>0" :percentage="percent" />
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>

    <hr>
    <UploadImage ref="upload" :limit="3" />
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
var cos = new COS({
  SecretId: 'AKIDmKF1OZnGwPeh9MR6bwUofX80vd6OBRM9',
  SecretKey: 'jCevznLEwOaVIYaNYaXGjxxlKfw8G9nX'
})
export default {
  data() {
    return {
      src: '',
      dialogImageUrl: '',
      dialogVisible: false,
      disabled: false,
      fileList: [],
      percent: 0
    }
  },
  mounted() {
    this.$refs.upload.list = [{ name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100' }, { name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100' }]
  },
  methods: {
    async upload(e) {
      console.dir(e.target.files[0])// 传到腾讯云
      const res = await cos.uploadFile({
        Bucket: '12334heima-1300357219', /* 填写自己的 bucket，必须字段 */
        Region: 'ap-shanghai', /* 存储桶所在地域，必须字段 */
        Key: Date.now() + e.target.files[0].name, /* 存储在桶里的对象键（例如1.jpg，a/b/test.txt），必须字段 */
        Body: e.target.files[0], // 上传文件对象
        SliceSize: 1024 * 1024 * 5 /* 触发分块上传的阈值，超过5MB 使用分块上传，小于5MB使用简单上传。可自行设置，非必须 */
      })
      console.log(res)
      this.src = 'https://' + res.Location
    },
    handleRemove(file) {
      console.log(file)
      // 先根据id找要删除这一项索引值 findIndex
      const index = this.fileList.findIndex(item => item.uid === file.uid)
      this.fileList.splice(index, 1)
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handleDownload(file) {
      console.log(file)
    },
    async handleChange(file, fileList) {
      console.log(file)
      const res = await cos.uploadFile({
        Bucket: '12334heima-1300357219', /* 填写自己的 bucket，必须字段 */
        Region: 'ap-shanghai', /* 存储桶所在地域，必须字段 */
        Key: Date.now() + file.raw.name, /* 存储在桶里的对象键（例如1.jpg，a/b/test.txt），必须字段 */
        Body: file.raw, // 上传文件对象
        SliceSize: 1024 * 1024 * 5, /* 触发分块上传的阈值，超过5MB 使用分块上传，小于5MB使用简单上传。可自行设置，非必须 */
        onProgress: (data) => {
          console.log(data)
          this.percent = Math.ceil(data.percent * 100)
        }
      })
      console.log(res)
      this.fileList.push({
        name: res.Location,
        url: 'https://' + res.Location
      })
      setTimeout(() => {
        this.percent = 0
      }, 1000)
    }
  }
}
</script>

<style scoped lang='scss'>
::v-deep .hidden .el-upload--picture-card{
  display: none !important;
}

::v-deep .el-upload-list__item.is-ready {
display: none;
}
</style>
